﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Diagnostics;
using System.Threading;
using System.Drawing;
using System.Windows.Forms;
using Styx;
using Styx.Combat.CombatRoutine;
using Styx.Helpers;
using Styx.Logic;
using Styx.Logic.Combat;
using Styx.Logic.Pathing;
using Styx.WoWInternals;
using Styx.WoWInternals.WoWObjects;
using Styx.Logic.POI;

namespace HarmoniousVigor
{
    public partial class Paladin
    {
        public bool retributionCombat()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (retributionPVPCombat())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (retributionDungeonCombat())
                    return true;
            }
            else
            {
                if (retributionQuestingCombat())
                    return true;
            }

            return false;
        }
        public bool retributionHeal()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (retributionPVPHeal())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (retributionDungeonHeal())
                    return true;
            }
            else
            {
                if (retributionQuestingHeal())
                    return true;
            }

            return false;
        }
        public bool retributionBuff()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (retributionPVPBuff())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (retributionDungeonBuff())
                    return true;
            }
            else
            {
                if (retributionQuestingBuff())
                    return true;
            }

            return false;
        }
        public bool retributionPullSpell()
        {
            if (Battlegrounds.IsInsideBattleground)
            {
                if (retributionPVPPullSpell())
                    return true;
            }
            else if (characterPlayer.IsInInstance)
            {
                if (retributionDungeonPullSpell())
                    return true;
            }
            else
            {
                if (retributionQuestingPullSpell())
                    return true;
            }

            return false;
        }

        public bool retributionMeleeCombatBehavior()
        {
            if (Utils.CastTarget("Hammer of Justice", characterPlayer.CurrentTarget))
                return true;
            if (Utils.GetEnemiesThisDistanceMe(8) > 2)
            {
                if (Utils.CastTarget("Divine Storm", characterPlayer.CurrentTarget))
                    return true;
            }
            else
            {
                if (Utils.CastTarget("Crusader Strike", characterPlayer.CurrentTarget))
                    return true;
            }
            if (Utils.CastTarget("Hammer of Wrath", characterPlayer.CurrentTarget))
                return true;
            if (HasFullHolyPower)
            {
                if (Utils.CastTarget("Templar's Verdict", characterPlayer.CurrentTarget))
                    return true;
            }
            if (characterPlayer.HasAura("The Art of War") && characterPlayer.Auras["The Art of War"].TimeLeft != new TimeSpan(0))
            {
                if (Utils.CastTarget("Exorcism", characterPlayer.CurrentTarget))
                    return true;
            }
            if (Utils.CastTarget("Judgement", characterPlayer.CurrentTarget))
                return true;
            if (Utils.CastTarget("Holy Wrath", characterPlayer.CurrentTarget))
                return true;
            if (Utils.GetEnemiesThisDistanceMe(10) > 3)
            {
                if (Utils.CastTarget("Consecration", characterPlayer.CurrentTarget))
                    return true;
            }

            return false;
        }
        public bool retributionRangedCombatBehavior()
        {
            if (!SpellManager.GlobalCooldown)
            {
                if (targetRange < 30)
                {
                    if (Utils.CastTarget("Hammer of Wrath", characterPlayer.CurrentTarget))
                        return true;
                }
                if (targetRange < judgementRange)
                {
                    if (Utils.CastTarget("Judgement", characterPlayer.CurrentTarget))
                        return true;
                }
                if (targetRange < 30)
                {
                    if (characterPlayer.HasAura("The Art of War") && characterPlayer.Auras["The Art of War"].TimeLeft != new TimeSpan(0))
                    {
                        if (Utils.CastTarget("Exorcism", characterPlayer.CurrentTarget))
                            return true;
                    }
                }
            }
            return false;
        }
        public bool retributionHealBehavior()
        {
            return false;
        }
        public bool retributionRootBehavior()
        {
            if (canCleanseRoot)
            {
                if (Utils.CastMe("Cleanse"))
                    return true;
            }

            if (Utils.CastMe("Hand of Freedom"))
                return true;
            return false;
        }
        public bool retributionInterruptBehavior()
        {
            if (characterPlayer.CurrentTarget.IsCasting
            && characterPlayer.CurrentTarget.CanInterruptCurrentSpellCast
            && characterPlayer.CurrentTarget.CurrentCastTimeLeft < new TimeSpan(0, 0, 0, 0, 2000))
            {
                if (Utils.CastTarget("Rebuke", characterPlayer.CurrentTarget))
                    return true;
            }
            return false;
        }
        public bool retributionCrowdControlBehavior()
        {
            WoWUnit repentanceTarget = ObjectManager.GetObjectsOfType<WoWUnit>(false, false).FirstOrDefault(unit => unit != characterPlayer.CurrentTarget
                       && !unit.IsFriendly && unit.Distance2D <= 30 && unit.Combat && unit.IsAlive && unit.Aggro
                       && (unit.CreatureType == WoWCreatureType.Demon || unit.CreatureType == WoWCreatureType.Dragon || unit.CreatureType == WoWCreatureType.Humanoid
                       || unit.CreatureType == WoWCreatureType.Undead || unit.CreatureType == WoWCreatureType.Giant));
            if (repentanceTarget != null)
            {
                if (Utils.CastTarget("Repentance", repentanceTarget))
                {
                    RepentanceTarget = repentanceTarget;
                    return true;
                }
            }
            return false;
        }
        public bool retributionRegularBehavior()
        {
            if (characterPlayer.CurrentTarget == null || characterPlayer.Mounted)
                return true;
            meleeMovement();
            faceTarget();
            AutoAttack();
            return false;
        }
        public void retributionRepentanceCheckBehavior()
        {
            if (characterPlayer.CurrentTarget == null)
            {
                WoWUnit optionalTarget = ObjectManager.GetObjectsOfType<WoWUnit>(false, false).FirstOrDefault(unit => !unit.IsFriendly
                    && unit.Distance2D <= 30 && unit.Combat && unit.IsAlive && unit.Aggro && unit != RepentanceTarget);
                if (optionalTarget == null)
                {
                    if (RepentanceTarget != null)
                    {
                        if (RepentanceTarget.Combat && RepentanceTarget.IsAlive && RepentanceTarget.Aggro)
                        {
                            Utils.Log("Cannot find another target, targetting repentanced target");
                            RepentanceTarget.Target();
                        }
                        else
                        {
                            Utils.Log("Repentance target is not valid");
                            RepentanceTarget = null;
                        }
                    }
                }
            }
        }
        public bool retributionCooldownBehavior()
        {
            if (PaladinSettings.useRetributionCooldowns && characterPlayer.Combat)
            {
                if (HasFullHolyPower)
                {
                    if (Utils.BuffMe("Zealotry"))
                        return true;
                }

                if (!SpellManager.GlobalCooldown && HasFullHolyPower && !characterPlayer.HasAura("Inquisition"))
                {
                    if (Utils.BuffMe("Inquisition"))
                        return true;
                }

                if (Utils.BuffMe("Avenging Wrath"))
                    return true;

                if (Utils.BuffMe("Guardian of Ancient Kings"))
                    return true;
            }

            return false;
        }
        public bool retributionThreatDropBehavior()
        {
            if (characterPlayer.ThreatInfo.RawPercent > 90)
            {
                if (Utils.CastMe("Hand of Salvation"))
                    return true;
            }

            return false;
        }
    }
}
